package com.technodo.play;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.json.JSONException;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.technodo.R;
import com.technodo.TechnodoMain;
import com.technodo.avatar.PlayerProfile;
import com.technodo.enums.PlayerType;
import com.technodo.gamedata.Director;
import com.technodo.gamedata.GameRules;
import com.technodo.gamedata.PlayerBoard;
import com.technodo.gamedata.Product;
import com.technodo.gamedata.Tech;
import com.technodo.utils.GenericDialogActivity;
import com.technodo.utils.GlobalPreferences;
import com.technodo.utils.StringConstants;
public class ClassicPlayActivity extends Activity {

	private String TAG = "ClassicPlayActivity"; 

	private Dialog dialog;
	private Dialog techDialog;
	private Dialog playerTechDialog;	
	private Dialog productTapDialog;
	private Dialog hireMgtDialog;
	private Dialog productListDialog;



	private int cost=0;
	private SeekBar costSeekBar;

	//variables to avoid calling of multiple instances of same dialog
	private boolean isTechDialogOpen =false;
	private boolean isPlayerTechDialogOpen =false;
	private boolean isProductTapDialogOpen =false;
	private boolean isHireMgtDialogOpen =false;
	private boolean startClicked= false;

	ImageView person_1_ImageView;
	ImageView person_2_ImageView;
	ImageView person_3_ImageView;
	ImageView person_4_ImageView;
	ImageView player1ImageView;
	ImageView player2ImageView;
	ImageView player3ImageView;
	ImageView player4ImageView;

	Button okDirector;
	Button okCancel;
	ViewFlipper viewFlipper;

	ArrayList<String> arrayList = null;
	ArrayList<String> arrayListHumanSpinner = null;

	ArrayList<String> arrayListFactorySpinner = null;
	ArrayList<String> arrayListEnggSpinner = null;
	//path up to images folder
	private static final String GET_PATH = "data/profiles/images/";

	private GameRules gameRules;

	// Listing of all player profiles
	private PlayerProfile[] playerProfiles;
	private int listIndexCalculator = 0;

	//Image Views of the grid elements
	ImageView baseImage;
	ImageView topLeft;
	ImageView topRight;
	ImageView bottomLeft;
	ImageView bottomRight;

	//Main Screen Image Views
	ImageView PatentTech;
	ImageView RecruitEngg;
	ImageView Factory;
	ImageView buyDirector;
	ImageView Advertise;
	ImageView CostCut;
	ImageView Productise;
	ImageView Exit;

	private ImageView productListImageView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.classic_play);
		//		View v = this.getCurrentFocus();
		viewFlipper = (ViewFlipper) findViewById(R.id.mainViewFlipper);

		// Game related information setup
		try {
			gameRules = new GameRules(GlobalPreferences.getGlobalPreferences().getDefaultDirectors(),
					GlobalPreferences.getGlobalPreferences().getDefaultProducts(), 
					GlobalPreferences.getGlobalPreferences().getDefaultTechs());
			// Reset the game with current date

			Date date = new Date();
			gameRules.resetGame(date);

			// Initialise the rules
			gameRules.initRules();
			Log.d(TAG,"total number of techs available : "+gameRules.getNumberOfAvailableTechs());

			// Place player profiles for various players
			loadPlayerProfiles();
			Log.d(TAG,"loadPlayerProfiles over");


		} catch (JSONException e) {
			Log.e(GlobalPreferences.TAG, "Error parsing json : " + e.getMessage());
			e.printStackTrace();
		} catch (IllegalStateException i) {
			Log.e(GlobalPreferences.TAG, "Error initialising game state : " + i.getMessage());
			i.printStackTrace();
		} catch (Exception f) {
			Log.e(GlobalPreferences.TAG, "Unknown Exception initialising game state : " + f.getMessage());
			f.printStackTrace();
		}
		// Choose the players to start the game
		startDialog();

		ImageView dollarImageView = (ImageView) findViewById(R.id.button13);
		dollarImageView.setImageResource(R.drawable.dollars);

		ImageView brandImageView = (ImageView) findViewById(R.id.button14);
		brandImageView.setImageResource(R.drawable.brand_value);

		ImageView factoryImageView = (ImageView) findViewById(R.id.button15);
		factoryImageView.setImageResource(R.drawable.factory);

		ImageView engineeringImageView = (ImageView) findViewById(R.id.button16);
		engineeringImageView.setImageResource(R.drawable.engineering);

		ImageView productListImageView = (ImageView)findViewById(R.id.button17);
		productListImageView.setImageResource(R.drawable.list);

		PatentTech = (ImageView) findViewById(R.id.PatentTech);
		RecruitEngg= (ImageView) findViewById(R.id.RecruitEngg);
		Factory= (ImageView) findViewById(R.id.Factory);
		buyDirector= (ImageView) findViewById(R.id.buyDirector);
		Advertise= (ImageView) findViewById(R.id.Advertise);
		CostCut= (ImageView) findViewById(R.id.CostCut);
		Productise= (ImageView) findViewById(R.id.Productise);
		Exit= (ImageView) findViewById(R.id.Exit);

		PatentTech.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Log.d(TAG,"got inside on click PatentTech");
				//				if(!viewFlipper.getCurrentView().getTag().toString().equalsIgnoreCase(viewFlipper.getChildAt(0).getTag().toString()))
				viewFlipper.setDisplayedChild(0);
			}
		});
		RecruitEngg.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Log.d(TAG,"got inside on click RecruitEngg");
				//				if(!viewFlipper.getCurrentView().getTag().toString().equalsIgnoreCase(viewFlipper.getChildAt(1).getTag().toString()))
				viewFlipper.setDisplayedChild(1);

			}
		});
		Factory.setOnClickListener(new OnClickListener() {

			public void onClick(View v) { 
				Log.d(TAG,"got inside on click Factory");
				Log.d(TAG,"viewFlipper.getCurrentView().getId() : "+viewFlipper.getDisplayedChild());
				viewFlipper.setDisplayedChild(2);

			}
		});
		buyDirector.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Log.d(TAG,"got inside on click HireMgt");
				Log.d(TAG,"viewFlipper.getCurrentView().getId() : "+viewFlipper.getDisplayedChild());
				viewFlipper.setDisplayedChild(3);
			}
		});
		Advertise.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Log.d(TAG,"got inside on click advertise");
				Log.d(TAG,"viewFlipper.getCurrentView().getId() : "+viewFlipper.getDisplayedChild());
				viewFlipper.setDisplayedChild(4);

			}
		});
		CostCut.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Log.d(TAG,"got inside on click CostCut");
				Log.d(TAG,"viewFlipper.getCurrentView().getId() : "+viewFlipper.getDisplayedChild());
				viewFlipper.setDisplayedChild(5);
				initCostCutPanel();
			}
		});
		Productise.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Log.d(TAG,"got inside on click Productise");
				Log.d(TAG,"viewFlipper.getCurrentView().getId() : "+viewFlipper.getDisplayedChild());
				viewFlipper.setDisplayedChild(6);

			}
		});
		Exit.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Log.d(TAG,"got inside on click Exit");
				finish();
			}
		});
		TextView directorCost = (TextView) findViewById(R.id.directorCost);
		//FIXME pass the director after popping from the stack
		//		directorCost.setText(gameRules.getCurrentPlayerBoard().costOfDirector(director));
		okDirector = (Button) findViewById(R.id.okDirector);
		okDirector.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Log.d(TAG,"okDirector clicked");
				if(isHireMgtDialogOpen == false)
					showHireMgt();
				isHireMgtDialogOpen = false;
			}
		});
	}
	/**
	 * initialising the 1st turn for human player 
	 */
	private void initInitialTurnOrder(){
		gameRules.addTurn(0);
		gameRules.setCurrentPlayerIndex(0);
	}
	/**
	 * initialising the grid view of the Patent-Tech screen
	 */
	private void initPatentTechGridView(){
		//Adding all the Techs to the Grid View
		List<Tech> list = new ArrayList<Tech>(); 
		list.addAll(gameRules.getAvailableTechs());
		Log.d(TAG,"Number of techs read from the json are : "+list.size());

		//Getting the views and setting the background images for the grid
		for(int i=0;i<4;i++){						// check the row of the image view grid
			for(int j=0;j<4;j++){ 					// check the column of the image view grid
				final Tech currentTech =  list.get(listIndexCalculator);

				//					Log.d(TAG,"row "+i+ "column : "+j) ;
				final View currentImageView = getGridElementView(i, j,0);
				baseImage = (ImageView) currentImageView.findViewById(R.id.base);
				bottomLeft = (ImageView) currentImageView.findViewById(R.id.bottomLeft);
				bottomRight = (ImageView) currentImageView.findViewById(R.id.bottomRight);
				topLeft = (ImageView) currentImageView.findViewById(R.id.topLeft);
				topRight = (ImageView) currentImageView.findViewById(R.id.topRight);
				baseImage.setBackgroundColor(currentTech.getColor().getValue());
				//					 Log.d(TAG,"color value : "+list.get(listIndexCalculator).getColor().getValue());
				placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+list.get(listIndexCalculator).getIconPath(), baseImage);

				//if the tech has already been bought then the tick or cross won't be visible
				if(currentTech.getPlayerBoard() == null)
					bottomRight.setVisibility(View.VISIBLE);

				//in case the current player doesnt have enough gold to buy the tech then a cross has to be displayed
				if (gameRules.getCurrentPlayerBoard().CostOfTech(currentTech) > gameRules.getCurrentPlayerBoard().getGold())
					bottomRight.setImageResource(R.drawable.cross);

				//place the image of the player in case the tech is already bought
				if(currentTech.getPlayerBoard() == null)
					bottomLeft.setVisibility(View.GONE);
				else {
					//place the image of the player who has bought the tech
					placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+currentTech.getPlayerBoard().getPlayerProfile().getIconPath(), bottomLeft);
				}
				baseImage.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						Log.d(TAG,"the button chosen is :"+currentTech.getName());
						if(isTechDialogOpen == false)
							techClicked(currentTech,currentImageView);
					}
				});
				//get the cost of the tech 
				//Log.d(TAG,"tech value : "+currentTech.getPlayerBoard().CostOfTech(currentTech));
				listIndexCalculator = listIndexCalculator +1;
			}
		}
	}
	private void showHireMgt(){

		hireMgtDialog = new Dialog(this);
		hireMgtDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		hireMgtDialog.setContentView(R.layout.hire_management);
		hireMgtDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
				WindowManager.LayoutParams.MATCH_PARENT);
		hireMgtDialog.show();
		Log.d(TAG,"hireMgtDialog" +hireMgtDialog.isShowing());
		isHireMgtDialogOpen= true;
		hireMgtDialog.setOnCancelListener( new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				hireMgtDialog.dismiss();
				isHireMgtDialogOpen = false;		
			}
		});
		ImageView cardImage1 = (ImageView) hireMgtDialog.findViewById(R.id.cardImage1);
		cardImage1.setImageResource(R.drawable.icon_prd_augmented_reality);

	}
	/**
	 * Shows current cost, current BV, current Factory level, current Engineering Level
	 * @param playerBoard
	 */
	private void initPlayerInfo(){

		Log.d(TAG,"initPlayerInfo");
		for(int i=0; i<gameRules.getNumberOfPlayingPlayers(); i++){

			Log.d(TAG," inside for initPlayerInfo");
			ImageView playerImageView = getPlayerTechCardView(0, i);
			final PlayerBoard clickedPB = gameRules.getCurrentPlayerBoard(i);
			Log.d(TAG," profile image path ="+clickedPB.getPlayerProfile().getIconPath());
			placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+GET_PATH+clickedPB.getPlayerProfile().getIconPath(), playerImageView);

			//To select a particular Player
			playerImageView.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					currentEngineeringBVFactory(clickedPB);
				}
			});
		}
	}
	/**
	 * To get current players Factory, Engineering and BV values and to display on the right pane. 
	 * @param clickedPB current PlayerBoards object
	 */
	private void currentEngineeringBVFactory(final PlayerBoard playerBoard){

		TextView player = (TextView)findViewById(R.id.playerName);
		player.setText(playerBoard.getPlayerProfile().getTitle());

		Log.d(TAG,"clicked player is "+playerBoard.getPlayerProfile().getName());
		Toast.makeText(getApplicationContext(), "Current Player is " +playerBoard.getPlayerProfile().getTitle(), Toast.LENGTH_SHORT).show();
		TextView dollarTextView = (TextView)findViewById(R.id.dollarsTextView);
		Log.d(TAG,"clicked players dollars "+playerBoard.getGold());
		dollarTextView.setText(playerBoard.getGold().toString());

		//To set Factory Image for current Player
		int currentFactoryValue = playerBoard.getFactory();
		initPlayerTechCards(playerBoard);
		ImageView factoryimageView = (ImageView)findViewById(R.id.button15);
		getFactoryLevelIcon(currentFactoryValue,factoryimageView);

		//To set Engineering Level for Current User
		int currentEnggValue = playerBoard.getEngg();
		ImageView EnggimageView = (ImageView)findViewById(R.id.button16);
		getEnggLevelIcon(currentEnggValue, EnggimageView);

		//To set BV for the current User
		TextView bVTextView = (TextView)findViewById(R.id.bvTextView);
		if(0 != playerBoard.getBrandValue()){
			bVTextView.setText(playerBoard.getBrandValue().toString());
		}
		else{
			bVTextView.setText("0");
		}
		
		// ImageView to show the product list of current player
		productListImageView = (ImageView)findViewById(R.id.button17);
		if(playerBoard.getProducts().size() != 0){
			Log.d(TAG, "product Size :" +playerBoard.getProducts().size());
			final ArrayList<String> productList = new ArrayList<String>();
			for(int i= 0; i<playerBoard.getProducts().size(); i++){
				Log.d(TAG, "in for current product is :"+playerBoard.getProducts().get(i));
				productList.add(playerBoard.getProducts().get(i).getTitle());
				productListImageView.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						productListDialog = new Dialog(ClassicPlayActivity.this);
						productListDialog.setTitle("Product List Owened By :"+playerBoard.getPlayerProfile().getTitle());
						//productListDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
						productListDialog.setContentView(R.layout.product_list);
						productListDialog.show();
						ListView productListView = (ListView)productListDialog.findViewById(R.id.productList1); 
						Log.d(TAG, "in onClick ProductList");
						ArrayAdapter<String> adapter = new ArrayAdapter<String>(ClassicPlayActivity.this, android.R.layout.simple_expandable_list_item_1, productList);
						productListView.setAdapter(adapter);
					}
				});
			}

		}
		else {
			Toast.makeText(getApplicationContext(), "No any product is built yet", Toast.LENGTH_SHORT).show();
		}
		
	}
	/**
	 * Show director cards owned by the player in the classic Play screen (on the right)
	 */
	private void initPlayerDirectors( final PlayerBoard playerBoard){
		if(playerBoard!=null && playerBoard.getDirectors() !=null){
			for( int i=0;i<playerBoard.getDirectors().size();i++){
				ImageView playerDirectorImageView = getPlayerTechCardView(2, i);
				final Director directorSelected = playerBoard.getDirectors().get(i);

				placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+directorSelected.getImagePath(), playerDirectorImageView);
			}
		} else Log.d(TAG,"playerboard or director cards in initPlayerDirectors are null");
	}
	/**
	 * Show tech cards owned by the player in the classic Play screen (on the right)
	 */
	private void initPlayerTechCards( final PlayerBoard playerBoard){
		//		if(playerBoard==null )
		//			Log.d(TAG,"playerboard in initPlayerTechCards are null");
		//		if(playerBoard.getTechCards().size()<=0 )
		//			Log.d(TAG,"playerBoard.getTechCards().size() in initPlayerTechCards is less than 0"+"name "+playerBoard.getPlayerProfile().getName());
		if(playerBoard!=null && playerBoard.getTechCards().size() >0){

			//remove the previous card images of the previous players and then redraw
			for(int i=0;i<4;i++)
			{
				ImageView playerTechCardImageView = getPlayerTechCardView(1, i);
				playerTechCardImageView.setImageResource(android.R.color.transparent);
			}

			Log.d(TAG,"initPlayerTechCards "+playerBoard.getTechCards().size() +"name "+playerBoard.getPlayerProfile().getName());
			for( int i=0;i<playerBoard.getTechCards().size();i++){
				ImageView playerTechCardImageView = getPlayerTechCardView(1, i);
				final Tech techSelected = playerBoard.getTechCards().get(i);

				placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+techSelected.getImagePath(), playerTechCardImageView);

				playerTechCardImageView.setOnClickListener(new View.OnClickListener() {
					public void onClick(View arg0) {
						if(isPlayerTechDialogOpen == false)
							playerTechClicked(techSelected,playerBoard);
					}
				});
			}
		} else Log.d(TAG,"playerboard or tech cards in initPlayerTechCards are null");
	}
	/**
	 * initialise the recruitEngg View
	 */
	private void initRecruitEnggPanel(){
		ImageView imageView=null;
		Button b = null;
		for(int i=0;i<gameRules.getNumberOfPlayingPlayers();i++)
		{
			//make the player icons visible
			imageView = getRecruitEnggView(i,0);
			placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+GET_PATH+gameRules.getCurrentPlayerBoard(i).getPlayerProfile().getIconPath(), imageView);
			imageView.setVisibility(View.VISIBLE);

			//columns will go till the player player[i] has the current engg level
			if(gameRules.getCurrentPlayerBoard(i).getEngg()!=null){
				for(int j=1;j<=gameRules.getCurrentPlayerBoard(i).getEngg();j++)
				{
					Log.d(TAG,"row no. "+i+"column : "+j);
					imageView = getRecruitEnggView(i,j);
					imageView.setVisibility(View.VISIBLE);
				}
			}
			if(gameRules.getCurrentPlayerBoard(i) == gameRules.getCurrentPlayerBoard())
			{
				b = getRecruitEnggButton(i);
				b.setVisibility(View.VISIBLE);
				b.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {

						//checks if a user can actually buy it or not
						if(gameRules.getCurrentPlayerBoard().getMovesLeft() >0 && gameRules.getCurrentPlayerBoard().IsEnggBuyable() && gameRules.getCurrentPlayerBoard().getEngg()<4){
							GlobalPreferences.playSound(getApplicationContext(), R.raw.engg_built_cheer);
							Log.d(TAG,"the engg is buyable"+gameRules.getCurrentPlayerBoard().getEngg() );
							gameRules.ActionBuyEngg(gameRules.getCurrentPlayerBoard());

							
							// update the turn order in case the current player's turn is over
							gameRules.updateCurrentPlayerIndex();

							currentEngineeringBVFactory(gameRules.getCurrentPlayerBoard());
							Log.d(TAG,"the engg is buyable after"+gameRules.getCurrentPlayerBoard().getEngg() );
							initRecruitEnggPanel();
						}
						else if(gameRules.getCurrentPlayerBoard().getMovesLeft() ==0){
							GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
							Toast.makeText(getApplicationContext(),"Sorry no more moves available ",Toast.LENGTH_SHORT).show();
						}
						else if(gameRules.getCurrentPlayerBoard().getEngg()>=4){
							GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
							Toast.makeText(getApplicationContext(),"Sorry you cannot buy more than 4 levels of engineering ",Toast.LENGTH_SHORT).show();
						}
						else if (!gameRules.getCurrentPlayerBoard().IsEnggBuyable()){
							GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
							Toast.makeText(getApplicationContext(),"Sorry you do not have enough gold to buy the next level of engg",Toast.LENGTH_SHORT).show();
						}
					}
				});
			}
		}
	}
	/**
	 * initialising the grid view of the Productise screen
	 * @param calledFrom : whether the method is called from productise(calledFrom = 6) or advertise (calledFrom = 4) screens
	 */
	private void initProductiseGridView(int calledFrom){

		//Getting the views and setting the background images for the grid
		for(int i=0;i<4;i++){						// check the row of the image view grid
			for(int j=0;j<4;j++){ 					// check the column of the image view grid

				final int row =i;
				final int column = j;
				final Product product =  gameRules.getFrontProductPlacingElement(i,j);
				//				Log.d(TAG,"row "+i+ "column : "+j) ;

				final View currentImageView = getGridElementView(i, j,calledFrom);

				baseImage = (ImageView) currentImageView.findViewById(R.id.base);
				bottomLeft = (ImageView) currentImageView.findViewById(R.id.bottomLeft);
				bottomRight = (ImageView) currentImageView.findViewById(R.id.bottomRight);
				topLeft = (ImageView) currentImageView.findViewById(R.id.topLeft);
				topRight = (ImageView) currentImageView.findViewById(R.id.topRight);
				// check if is product buyable and if false
				//				bottomRight.setImageResource(R.drawable.cross);
				topLeft.setVisibility(View.VISIBLE);
				topRight.setVisibility(View.VISIBLE);

				placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+gameRules.getTech(product.getTech1name()).getIconPath(), topLeft);
				topLeft.setBackgroundColor(gameRules.getTech(product.getTech1name()).getColor().getValue());

				placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+gameRules.getTech(product.getTech2name()).getIconPath(), topRight);
				topRight.setBackgroundColor(gameRules.getTech(product.getTech2name()).getColor().getValue());
				getFactoryLevelIcon(product.getFactory(),bottomLeft);
				getEnggLevelIcon(product.getEngg(),bottomRight);
				placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+product.getIconPath(), baseImage);
				//make the row and column transparent for advertised and subsidised products
				if((gameRules.getSubsidy() == j || gameRules.getAdvertisedRows() == i) && calledFrom ==4)   
				{
					Log.d(TAG,"row and column "+ i + j +" alpha "+currentImageView.getId());
					Log.d(TAG,"row and column "+ gameRules.getSubsidy() + gameRules.getAdvertisedRows());

					//				baseImage.setAlpha(90);

					//FIXME : make the baseimage visibility light
					baseImage.setVisibility(View.INVISIBLE);
				}
				//				baseImage.setImageResource(R.drawable.icon_tech_green_secure_chip);
				baseImage.setOnClickListener(new OnClickListener() {

					public void onClick(View v) { 
						if(isProductTapDialogOpen ==false) {
							productTapDialog = new Dialog(ClassicPlayActivity.this);
							isProductTapDialogOpen =true;
							productTapDialog.setOnCancelListener( new OnCancelListener() {

								public void onCancel(DialogInterface dialog) {
									productTapDialog.dismiss();
									isProductTapDialogOpen = false;		
								}
							});
							productTapDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
									WindowManager.LayoutParams.MATCH_PARENT);
							productTapDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
							productTapDialog.setContentView(R.layout.product_tap);
							productTapDialog.show();

							TextView tech1 = (TextView) productTapDialog.findViewById(R.id.tech1);
							TextView tech1Value = (TextView) productTapDialog.findViewById(R.id.tech1Value);
							TextView tech2 = (TextView) productTapDialog.findViewById(R.id.tech2);
							TextView tech2Value = (TextView) productTapDialog.findViewById(R.id.tech2Value);

							TextView baseProfit= (TextView) productTapDialog.findViewById(R.id.baseProfitValue);
							TextView govtSubsidy = (TextView) productTapDialog.findViewById(R.id.govtSubsidyValue);
							TextView marketHotness= (TextView) productTapDialog.findViewById(R.id.marketHotnessValue);

							TextView totalProfitValue= (TextView) productTapDialog.findViewById(R.id.totalProfitValue);

							TextView player1share= (TextView) productTapDialog.findViewById(R.id.player1Share);
							TextView player1shareValue= (TextView) productTapDialog.findViewById(R.id.player1ShareValue);
							TextView player2share= (TextView) productTapDialog.findViewById(R.id.player2Share);
							TextView player2shareValue= (TextView) productTapDialog.findViewById(R.id.player2ShareValue);
							TextView player3share= (TextView) productTapDialog.findViewById(R.id.player3Share);
							TextView player3shareValue= (TextView) productTapDialog.findViewById(R.id.player3ShareValue);
							TextView player4share= (TextView) productTapDialog.findViewById(R.id.player4Share);
							TextView player4shareValue= (TextView) productTapDialog.findViewById(R.id.player4ShareValue);

							tech1.setText(gameRules.getTech(product.getTech1name()).getName());
							tech2.setText(gameRules.getTech(product.getTech2name()).getName());

							if(null == gameRules.getTech(product.getTech1name()).getPlayerBoard())
								tech1Value.setText("none");
							else 
								tech1Value.setText(gameRules.getTech(product.getTech1name()).getPlayerBoard().getPlayerProfile().getName());

							if(null == gameRules.getTech(product.getTech2name()).getPlayerBoard())
								tech2Value.setText("none");
							else 
								tech2Value.setText(gameRules.getTech(product.getTech2name()).getPlayerBoard().getPlayerProfile().getName());

							baseProfit.setText("$"+product.getProfit().toString()+" Million");
							Integer subsidy;
							if(column == gameRules.getSubsidy())
								subsidy = product.getProfit();
							else subsidy=0;

							govtSubsidy.setText("$"+subsidy.toString()+" Million");

							Integer hotness;
							if(row == gameRules.getAdvertisedRows())
								hotness = product.getProfit()+subsidy;
							else hotness=0;

							marketHotness.setText("$"+hotness.toString()+" Million");
							Integer total_profit = product.getProfit()+subsidy+hotness;
							totalProfitValue.setText("$"+total_profit.toString()+" Million");


							final Spinner factorySpinner = (Spinner) productTapDialog.findViewById(R.id.factorySpinner);
							arrayListFactorySpinner= new ArrayList<String>();
							for(int i =0 ;i <gameRules.whoHasFactory(product.getFactory()).size() ; i++)
							{
								arrayListFactorySpinner.add(gameRules.whoHasFactory(product.getFactory()).get(i).getPlayerProfile().getName());
							}
							Log.d(TAG," size of arrayListFactorySpinner"+arrayListFactorySpinner.size() + gameRules.whoHasFactory(product.getFactory()).size());

							if(arrayListFactorySpinner.size() == 0)
								arrayListFactorySpinner.add("none");

							ArrayAdapter<String> listAdapterFactorySpinner = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayListFactorySpinner);
							listAdapterFactorySpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
							factorySpinner.setAdapter(listAdapterFactorySpinner);

							//for (int i =0; i<gameRules.getNumberOfPlayingPlayers();i++)


							final Spinner enggSpinner = (Spinner) productTapDialog.findViewById(R.id.enggSpinner);
							arrayListEnggSpinner= new ArrayList<String>();
							for(int i =0 ;i <gameRules.whoHasEngg(product.getEngg()).size() ; i++)
							{
								arrayListEnggSpinner.add(gameRules.whoHasEngg(product.getEngg()).get(i).getPlayerProfile().getName());
							}
							Log.d(TAG," size of arrayListEnggSpinner"+arrayListEnggSpinner.size() + gameRules.whoHasEngg(product.getEngg()).size());

							if(arrayListEnggSpinner.size() == 0)
								arrayListEnggSpinner.add("none");

							ArrayAdapter<String> listAdapterEnggSpinner = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayListEnggSpinner);
							listAdapterFactorySpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
							enggSpinner.setAdapter(listAdapterEnggSpinner);


							PlayerBoard PBengg=null;
							PlayerBoard PBfactory=null;

							if(!arrayListEnggSpinner.get(0).toString().equalsIgnoreCase("none")){	
								for(int i =0 ;i <gameRules.whoHasEngg(product.getEngg()).size() ; i++)
								{
									if(gameRules.whoHasEngg(product.getEngg()).get(i).getPlayerProfile().getName().equalsIgnoreCase(enggSpinner.getSelectedItem().toString()) )
										PBengg=gameRules.whoHasEngg(product.getEngg()).get(i);
								}

							}
							if(!arrayListFactorySpinner.get(0).toString().equalsIgnoreCase("none") ){	
								for(int i =0 ;i <gameRules.whoHasFactory(product.getFactory()).size() ; i++)
								{
									if(gameRules.whoHasFactory(product.getFactory()).get(i).getPlayerProfile().getName().equalsIgnoreCase(factorySpinner.getSelectedItem().toString()) )
										PBfactory=gameRules.whoHasFactory(product.getFactory()).get(i);
								}
							}
							ImageView productImage = (ImageView) productTapDialog.findViewById(R.id.productImage);
							placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+product.getImagePath(), productImage);


							Integer share;

							//atleast 1 player has to be there in order to start the game
							player1share.setText(gameRules.getCurrentPlayerBoard(0).getPlayerProfile().getName());
							share = gameRules.calculatePlayerShare(product, gameRules.getCurrentPlayerBoard(0), total_profit, gameRules.getCurrentPlayerBoard(), PBengg, PBfactory) ;
							player1shareValue.setText("$"+share.toString()+" Million");

							if(gameRules.getCurrentPlayerBoard(1) != null)
							{

								player2share.setVisibility(View.VISIBLE);
								player2shareValue.setVisibility(View.VISIBLE);
								player2share.setText(gameRules.getCurrentPlayerBoard(1).getPlayerProfile().getName());
								share = gameRules.calculatePlayerShare(product, gameRules.getCurrentPlayerBoard(1), total_profit, gameRules.getCurrentPlayerBoard(), PBengg, PBfactory) ;
								player2shareValue.setText("$"+share.toString()+" Million");
							}

							if(gameRules.getCurrentPlayerBoard(2) != null)
							{
								player3share.setVisibility(View.VISIBLE);
								player3shareValue.setVisibility(View.VISIBLE);
								share = gameRules.calculatePlayerShare(product, gameRules.getCurrentPlayerBoard(2), total_profit, gameRules.getCurrentPlayerBoard(), PBengg, PBfactory) ;
								player3shareValue.setText("$"+share.toString()+" Million");
								player3share.setText(gameRules.getCurrentPlayerBoard(2).getPlayerProfile().getName());
							}

							if(gameRules.getCurrentPlayerBoard(3) != null)
							{
								player4share.setVisibility(View.VISIBLE);
								player4shareValue.setVisibility(View.VISIBLE);
								share = gameRules.calculatePlayerShare(product, gameRules.getCurrentPlayerBoard(3), total_profit, gameRules.getCurrentPlayerBoard(), PBengg, PBfactory) ;
								player4shareValue.setText("$"+share.toString()+" Million");
								player4share.setText(gameRules.getCurrentPlayerBoard(3).getPlayerProfile().getName());
							}


							Button CancelProduct = (Button) productTapDialog.findViewById(R.id.cancelProduct);
							CancelProduct.setOnClickListener(new View.OnClickListener() {

								public void onClick(View v) {
									productTapDialog.dismiss();
									isProductTapDialogOpen =false;
								}
							});
							Button BuildProduct = (Button) productTapDialog.findViewById(R.id.buildProduct);
							BuildProduct.setOnClickListener(new View.OnClickListener() {

								public void onClick(View v) {
									PlayerBoard enggPB = null;
									PlayerBoard factoryPB= null;

									//check if the selected item is none or not
									Boolean isProductBuildable = false;
									if(!arrayListEnggSpinner.get(0).toString().equalsIgnoreCase("none") && !arrayListFactorySpinner.get(0).toString().equalsIgnoreCase("none") ){	
										isProductBuildable = true;
										Log.d(TAG,"buying product in isProductBuildable " +isProductBuildable);
										for(int i =0 ;i <gameRules.whoHasEngg(product.getEngg()).size() ; i++)
										{
											if(gameRules.whoHasEngg(product.getEngg()).get(i).getPlayerProfile().getName().equalsIgnoreCase(enggSpinner.getSelectedItem().toString()) )
												enggPB=gameRules.whoHasEngg(product.getEngg()).get(i);
										}
										for(int i =0 ;i <gameRules.whoHasFactory(product.getFactory()).size() ; i++)
										{
											if(gameRules.whoHasFactory(product.getFactory()).get(i).getPlayerProfile().getName().equalsIgnoreCase(factorySpinner.getSelectedItem().toString()) )
												factoryPB=gameRules.whoHasFactory(product.getFactory()).get(i);
										}
									}
									Log.d(TAG,"buying product in after isProductBuildable " +isProductBuildable);
									if(isProductBuildable == true && gameRules.IsProductBuyable(gameRules.getTech(product.getTech1name()),
											gameRules.getTech(product.getTech2name()), product.getEngg(), product.getFactory()) == true){
										/*Log.d(TAG,"buying product "+ product.getName() + "for player board "+gameRules.getCurrentPlayerIndex() + "gameRules.getTech(product.getTech1name()).getPlayerBoard() "+ 
												gameRules.getTech(product.getTech1name()).getPlayerBoard().getPlayerProfile().getName() + "gameRules.getTech(product.getTech2name()).getPlayerBoard() "+ 
												gameRules.getTech(product.getTech2name()).getPlayerBoard().getPlayerProfile().getName() +"enggPB "+enggPB.getPlayerProfile().getName()+"factoryPB "+factoryPB.getPlayerProfile().getName()+ "row "+row + "column "+column );*/

										gameRules.ActionProductise(product, gameRules.getCurrentPlayerBoard(), gameRules.getTech(product.getTech1name()).getPlayerBoard(), gameRules.getTech(product.getTech2name()).getPlayerBoard(),
												enggPB, factoryPB, row, column);
										// update the turn order in case the current player's turn is over
										gameRules.updateCurrentPlayerIndex();
										Toast.makeText(getApplicationContext(),"You built this product ",Toast.LENGTH_SHORT).show();
										productTapDialog.dismiss();
										isProductTapDialogOpen =false;
									}
									else {
										GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
										Toast.makeText(getApplicationContext(),"Sorry you cannot build this product ",Toast.LENGTH_SHORT).show();
									}
								}
							});
						}
						else {
							Log.d(TAG,"another instance of productTapDialog is already running ");
						}
					}

				});
			}
		}
	}
	public void getFactoryLevelIcon(int factoryLevel, ImageView factoryImage)
	{
		switch(factoryLevel){
		case 0:
			factoryImage.setImageResource(R.drawable.factory_0);
			break;
		case 1 :
			factoryImage.setImageResource(R.drawable.factory_1);
			break;
		case 2:
			factoryImage.setImageResource(R.drawable.factory_2);
			break;
		case 3:
			factoryImage.setImageResource(R.drawable.factory_3);
			break;
		case 4:
			factoryImage.setImageResource(R.drawable.factory_4);
			break;
		}
	}
	public void getEnggLevelIcon(int enggLevel, ImageView enggImage)
	{
		switch(enggLevel){
		case 0:
			enggImage.setImageResource(R.drawable.engineering_0);
			break;
		case 1 :
			enggImage.setImageResource(R.drawable.engineering_1);
			break;
		case 2:
			enggImage.setImageResource(R.drawable.engineering_2);
			break;
		case 3:
			enggImage.setImageResource(R.drawable.engineering_3);
			break;
		case 4:
			enggImage.setImageResource(R.drawable.engineering_4);
			break;
		}
	}
	/**
	 * 
	 * @param row a single row signifies a player
	 * @return button to buy engineering
	 */
	public Button getFactoryButton(int row){
		Button temp = null ;
		switch (row){
		case 0 : temp = (Button) findViewById(R.id.buyFactoryButton05);
		break;
		case 1 : temp = (Button)findViewById(R.id.buyFactoryButton15);
		break;
		case 2 : temp = (Button)findViewById(R.id.buyFactoryButton25);
		break;
		case 3 : temp = (Button)findViewById(R.id.buyFactoryButton35);
		break;
		default : Log.d(TAG,"unknown row value found");
		}
		return temp;
	}
	/**
	 * 
	 * @param row a single row signifies a player
	 * @return button to buy engineering
	 */
	public Button getRecruitEnggButton( int row){
		Button temp = null ;
		switch (row){
		case 0 : temp = (Button) findViewById(R.id.buyEnggButton05);
		break;
		case 1 : temp = (Button)findViewById(R.id.buyEnggButton15);
		break;
		case 2 : temp = (Button)findViewById(R.id.buyEnggButton25);
		break;
		case 3 : temp = (Button)findViewById(R.id.buyEnggButton35);
		break;
		default : Log.d(TAG,"unknown row value found");
		}
		return temp;
	}
	/**
	 * initialise the Factory View
	 */
	private void initFactoryPanel(){
		ImageView imageView=null;
		Button b= null;

		for(int i=0;i<gameRules.getNumberOfPlayingPlayers();i++)
		{
			//make the player icons visible
			imageView = getFactoryView(i,0);
			placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+GET_PATH+gameRules.getCurrentPlayerBoard(i).getPlayerProfile().getIconPath(), imageView);
			imageView.setVisibility(View.VISIBLE);

			//columns will go till the player player[i] has the current engg level
			if(gameRules.getCurrentPlayerBoard(i).getEngg()!=null){
				for(int j=1;j<=gameRules.getCurrentPlayerBoard(i).getFactory();j++)
				{
					imageView = getFactoryView(i,j);
					imageView.setVisibility(View.VISIBLE);
				}
			}
			if(gameRules.getCurrentPlayerBoard(i) == gameRules.getCurrentPlayerBoard())
			{
				b = getFactoryButton(i);
				b.setVisibility(View.VISIBLE);
				b.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {

						//checks if a user can actually buy it or not
						if(gameRules.getCurrentPlayerBoard().getMovesLeft() >0 && gameRules.getCurrentPlayerBoard().IsFactoryBuyable() && gameRules.getCurrentPlayerBoard().getFactory()<4){
							GlobalPreferences.playSound(getApplicationContext(), R.raw.factory_built_sound);
							Log.d(TAG,"the factory is buyable"+gameRules.getCurrentPlayerBoard().getFactory() );
							gameRules.ActionBuyFactory(gameRules.getCurrentPlayerBoard());

							
							// update the turn order in case the current player's turn is over
							gameRules.updateCurrentPlayerIndex();

							currentEngineeringBVFactory(gameRules.getCurrentPlayerBoard());
							Log.d(TAG,"the factory is buyable after"+gameRules.getCurrentPlayerBoard().getFactory() );
							initFactoryPanel();

						}
						else if(gameRules.getCurrentPlayerBoard().getMovesLeft() ==0){
							GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
							Toast.makeText(getApplicationContext(),"Sorry no more moves available ",Toast.LENGTH_SHORT).show();
						}
						else if(gameRules.getCurrentPlayerBoard().getFactory()>=4){
							GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
							Toast.makeText(getApplicationContext(),"Sorry you cannot buy more than 4 levels of factory ",Toast.LENGTH_SHORT).show();
						}
						else if (!gameRules.getCurrentPlayerBoard().IsFactoryBuyable()){
							GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
							Toast.makeText(getApplicationContext(),"Sorry you do not have enough gold to buy the next level of factory",Toast.LENGTH_SHORT).show();
						}
					}
				});
			}
		}

	}
	/**
	 * put the image in the given image view
	 * @param path : path to the image
	 * @param imageView : image view where the image has to be placed
	 */
	private void placeImages(String path,ImageView imageView) {
		Bitmap bitmap = BitmapFactory.decodeFile(path);
		if(bitmap !=null && imageView != null)
			imageView.setImageBitmap(bitmap);
		else throw new IllegalStateException("bitmap is null, looking in path : "+path) ;

		// FIXME: Should throw exception here
		//		Log.d(TAG, "Image Size : [" + bitmap.getHeight() + ":" + bitmap.getWidth() + "]");
		//		Log.d(TAG, "Image View Size : [" + imageView.getHeight() + ":" + imageView.getWidth() + "]");
	}
	/**
	 * show the dialog if a tech is clicked from the tech grid
	 * @param tempTech : the Tech clicked
	 * @param tempView : the complete View that the image is in(including the corner images : tick, dollar, player, brand value) 
	 */
	private void playerTechClicked( Tech tempTech,PlayerBoard PB) {

		playerTechDialog = new Dialog(this);
		isPlayerTechDialogOpen = true;
		playerTechDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		playerTechDialog.setContentView(R.layout.tech_player_clicked);
		playerTechDialog.setOnCancelListener( new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				playerTechDialog.dismiss();
				isPlayerTechDialogOpen = false;		
			}
		});

		ImageView cardImage = (ImageView) playerTechDialog.findViewById(R.id.CardImage);
		placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+tempTech.getImagePath(), cardImage);
		Button okButton = (Button) playerTechDialog.findViewById(R.id.okButton);

		TextView greenTechCards = (TextView) playerTechDialog.findViewById(R.id.greenTechCards);
		TextView greenNetBV = (TextView) playerTechDialog.findViewById(R.id.greenNetBV);
		TextView blueTechCards = (TextView) playerTechDialog.findViewById(R.id.blueTechCards);
		TextView blueNetBV = (TextView) playerTechDialog.findViewById(R.id.blueNetBV);
		TextView redTechCards = (TextView) playerTechDialog.findViewById(R.id.redTechCards);
		TextView redNetBV = (TextView) playerTechDialog.findViewById(R.id.redNetBV);
		TextView yellowTechCards = (TextView) playerTechDialog.findViewById(R.id.yellowTechCards);
		TextView yellowNetBV = (TextView) playerTechDialog.findViewById(R.id.yellowNetBV);

		//fill up the calculated brand values made from the techs owned
		greenTechCards.setText(Integer.toString(gameRules.getCurrentPlayerBoard().getGreenColorTechs()));
		greenNetBV.setText(Integer.toString(gameRules.getCurrentPlayerBoard().getBV(gameRules.getCurrentPlayerBoard().getGreenColorTechs())));

		blueTechCards.setText(Integer.toString(gameRules.getCurrentPlayerBoard().getBlueColorTechs()));
		blueNetBV.setText(Integer.toString(gameRules.getCurrentPlayerBoard().getBV(gameRules.getCurrentPlayerBoard().getBlueColorTechs())));

		redTechCards.setText(Integer.toString(gameRules.getCurrentPlayerBoard().getRedColorTechs()));
		redNetBV.setText(Integer.toString(gameRules.getCurrentPlayerBoard().getBV(gameRules.getCurrentPlayerBoard().getRedColorTechs())));

		yellowTechCards.setText(Integer.toString(gameRules.getCurrentPlayerBoard().getYellowColorTechs()));
		yellowNetBV.setText(Integer.toString(gameRules.getCurrentPlayerBoard().getBV(gameRules.getCurrentPlayerBoard().getYellowColorTechs())));

		okButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
				playerTechDialog.dismiss();
				isPlayerTechDialogOpen = false;
			}
		});
		playerTechDialog.show();
	}
	/**
	 * show the dialog if a tech is clicked from the tech grid
	 * @param tempTech : the Tech clicked
	 * @param tempView : the complete View that the image is in(including the corner images : tick, dollar, player, brand value) 
	 */
	private void techClicked(final Tech tempTech,final View tempView) {

		techDialog = new Dialog(this);
		isTechDialogOpen = true;
		Log.d(TAG,"isTechDialogopen start of techclicked " +isTechDialogOpen);
		//		Log.d(TAG, "Inside techClicked Dialog");
		techDialog.setOnCancelListener( new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
				techDialog.dismiss();
				isTechDialogOpen = false;		
			}
		});
		techDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		techDialog.setContentView(R.layout.tech_popup);
		ImageView techCardImage = (ImageView) techDialog.findViewById(R.id.techCardImage);
		final TextView Cost = (TextView) techDialog.findViewById(R.id.Cost);
		placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+tempTech.getImagePath(), techCardImage);
		Button cancel = (Button) techDialog.findViewById(R.id.cancelBuying);
		Button buyTech = (Button) techDialog.findViewById(R.id.buyTech);
		Button ok = (Button) techDialog.findViewById(R.id.ok);
		cancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
				techDialog.dismiss();
				isTechDialogOpen = false;
			}
		});
		ok.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
				techDialog.dismiss();
				isTechDialogOpen = false;
			}
		});
		//if the selected tech is already bought
		if(tempTech.getPlayerBoard()!=null)
		{
			Cost.setText("This tech has been bought by "+tempTech.getPlayerBoard().getPlayerProfile().getName());
			buyTech.setVisibility(View.GONE);
			cancel.setVisibility(View.GONE);
			ok.setVisibility(View.VISIBLE);

		}
		//update the cost of the tech in the dialog box
		else 
			Cost.setText("Cost of the tech is : "+gameRules.getCurrentPlayerBoard().CostOfTech(tempTech)+"$");

		buyTech.setOnClickListener(new OnClickListener() {			

			public void onClick(View v) {
				Log.d(TAG,"value of temp is  "+ gameRules.getCurrentPlayerBoard());
				if(tempTech.getPlayerBoard()==null)
				{
					//Log.d(TAG,"left moves before"+"for pb"+gameRules.getCurrentPlayerBoard().getPlayerProfile().getName()+ gameRules.getCurrentPlayerBoard().getMovesLeft());
					//checks if a user can actually buy it or not
					if(gameRules.getCurrentPlayerBoard().getMovesLeft() >0 && gameRules.getCurrentPlayerBoard().IsTechBuyable(tempTech)){
						gameRules.ActionBuyTech(gameRules.getCurrentPlayerBoard(), tempTech);
						//Log.d(TAG,"left moves after"+"for pb"+gameRules.getCurrentPlayerBoard().getPlayerProfile().getName()+gameRules.getCurrentPlayerBoard().getMovesLeft());

						// refresh the screen if a tech is bought
						//						initGridView();
						increaseCountofColorTechs(tempTech.getColor().getName());
						//						Log.d(TAG,"total cards include : green :"+gameRules.getCurrentPlayerBoard().getGreenColorTechs()+" blue : "+gameRules.getCurrentPlayerBoard().getBlueColorTechs()+" red :"+gameRules.getCurrentPlayerBoard().getRedColorTechs()+" Yellow :"+gameRules.getCurrentPlayerBoard().getYellowColorTechs());
						techDialog.dismiss();
						isTechDialogOpen= false;
						//Log.d(TAG,"isTechDialogopen if" +isTechDialogopen);
						initPlayerTechCards(gameRules.getCurrentPlayerBoard());
						updateGridView(tempTech,tempView);			
						// update the turn order in case the current player's turn is over
						if (gameRules.getCurrentPlayerBoard().getMovesLeft() == 0)
							gameRules.updateCurrentPlayerIndex();
						GenericDialogActivity dialogActivity = new GenericDialogActivity(ClassicPlayActivity.this,tempTech.getTitle(),tempTech.getPlayerBoard().getPlayerProfile().getName(),tempTech.getIconPath(),tempTech.getQuote(),tempTech.getQuoter(),R.raw.advertise_sound);
						dialogActivity.startGenericDialog();
						dialogActivity.show();
					}
					//player cannot afford buying the tech
					else if (!gameRules.getCurrentPlayerBoard().IsTechBuyable(tempTech)) {
						bottomRight.setVisibility(View.VISIBLE);
						bottomRight.setImageResource(R.drawable.cross);
						//						Log.d(TAG,"isTechDialogopen else if" +isTechDialogopen);
						GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
						Toast.makeText(getApplicationContext(),"Sorry you cannot afford this tech ",Toast.LENGTH_SHORT).show();
					}
					else {
						GlobalPreferences.playSound(getApplicationContext(), R.raw.turn_over);
						Toast.makeText(getApplicationContext(),"Sorry no more moves left ",Toast.LENGTH_SHORT).show();

						techDialog.dismiss();
						isTechDialogOpen = false;
						//						Log.d(TAG,"isTechDialogopen else" +isTechDialogopen);
					}
				}
			}
		});
		techDialog.show();
	}

	/**
	 * increment the count of techs of respective color 
	 * @param color : color of tech chosen
	 */
	private void increaseCountofColorTechs(String color){
		if(color.equalsIgnoreCase("green"))
			gameRules.getCurrentPlayerBoard().updateGreenColorTechs();
		if(color.equalsIgnoreCase("blue"))
			gameRules.getCurrentPlayerBoard().updateBlueColorTechs();
		if(color.equalsIgnoreCase("red"))
			gameRules.getCurrentPlayerBoard().updateRedColorTechs();
		if(color.equalsIgnoreCase("yellow"))
			gameRules.getCurrentPlayerBoard().updateYellowColorTechs();
	}
	/** 
	 * update the element (tech) and the corner images according to the status of tech(if bought or not)
	 * @param currentTech : the tech selected
	 * @param currentView : the view of selected tech
	 */
	private void updateGridView(Tech currentTech,View currentView){
		Log.d(TAG,"in updategridview()"+currentView.getId());
		bottomLeft = (ImageView) currentView.findViewById(R.id.bottomLeft);
		bottomRight = (ImageView) currentView.findViewById(R.id.bottomRight);
		topLeft = (ImageView) currentView.findViewById(R.id.topLeft);
		topRight = (ImageView) currentView.findViewById(R.id.topRight);
		baseImage = (ImageView) currentView.findViewById(R.id.base);
		//place the image of the player who has bought the tech
		//		Log.d(TAG,"looking for player image in : "+GlobalPreferences.getGlobalPreferences().getDataDir()+GET_PATH+currentTech.getPlayerBoard().getPlayerProfile().getIconPath());
		placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+GET_PATH+currentTech.getPlayerBoard().getPlayerProfile().getIconPath(), bottomLeft);
		bottomLeft.setVisibility(View.VISIBLE);
		topRight.setVisibility(View.GONE);
		bottomRight.setVisibility(View.INVISIBLE);
	}
	/**
	 * initial dialog : makes the user to select the players and start the game 
	 */
	private void startDialog() {

		dialog = new Dialog(this);
		//		Log.d(TAG, "Inside custom_dialog Dialog");
		arrayList= new ArrayList<String>();
		arrayListHumanSpinner= new ArrayList<String>();
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.custom_dialog);
		Button startButton = (Button) dialog.findViewById(R.id.buttonStart);

		dialog.setOnCancelListener( new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				dialog.dismiss();
				Intent intent = new Intent(ClassicPlayActivity.this, TechnodoMain.class);
				startActivity(intent);

			}
		});
		final Spinner humanSpinner = (Spinner)dialog.findViewById(R.id.humanSpinner);
		final Spinner aiSpinner1 = (Spinner)dialog.findViewById(R.id.aiSpinner1);
		final Spinner aiSpinner2 = (Spinner)dialog.findViewById(R.id.aiSpinner2);
		final Spinner aiSpinner3 = (Spinner)dialog.findViewById(R.id.aiSpinner3);

		for(PlayerProfile p : playerProfiles) {
			if(p.getPlayerType() == PlayerType.HUMAN)
				arrayListHumanSpinner.add(p.getTitle());
		}
		for(PlayerProfile p : playerProfiles) {
			arrayList.add(p.getTitle());
		}
		Log.d(TAG,"size of the arraylist is : "+arrayList.size());
		Log.d(TAG,"size of the arrayListHumanSpinner is : "+arrayListHumanSpinner.size());
		//appending none to the profile lists
		arrayList.add(arrayList.size(),"none");

		ArrayAdapter<String> listAdapterHumanSpinner = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, arrayListHumanSpinner);
		listAdapterHumanSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		//ListAdapter to show values present in the Aarry in the list format.
		ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, arrayList);
		listAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		dialog.show();
		//Spinners
		humanSpinner.setAdapter(listAdapterHumanSpinner);
		aiSpinner1.setAdapter(listAdapter);
		aiSpinner2.setAdapter(listAdapter);
		aiSpinner3.setAdapter(listAdapter);

		//Item selected from the list by Spinner 1
		humanSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
				Log.d(TAG,"current selected Item for human Spinner: "+i);
				//Call the method to change the image according to the position of the item in the list.
				setHumanImage(i);
			}
			public void onNothingSelected(AdapterView<?> arg0) {
				Log.d(TAG, "Nothing selected in human spinner ");
			} 
		});

		//Item selected from the list by Spinner 1
		aiSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int i, long l) {
				Log.d(TAG,"current selected Item for aiSpinner1: "+i);
				//Call the method to change the image according to the position of the item in the list.
				setAIImage(1,i);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				Log.d(TAG, "Nothing selected in spinner 1");
			}
		});

		//Item selected from the list by Spinner 1
		aiSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int i, long l) {
				Log.d(TAG,"current selected Item for aiSpinner2: "+i);
				//Call the method to change the image according to the position of the item in the list.
				setAIImage(2,i);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				Log.d(TAG, "Nothing selected in spinner 2");
			}
		});

		aiSpinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int i, long l) {
				Log.d(TAG,"current selected Item for aiSpinner3: "+i);
				//Call the method to change the image according to the position of the item in the list.
				setAIImage(3,i);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				Log.d(TAG, "Nothing selected in spinner 3");
			}
		});

		startButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				//to avoid start button being clicked twice
				if(startClicked == false){
					startClicked = true;
					PlayerProfile playerToBeAdded = null;

					if ( v.getId() == R.id.buttonStart){

						//add the human player to playing players
						gameRules.addTurn(gameRules.getNumberOfPlayingPlayers());
						playerToBeAdded = getPlayerProfile((String) humanSpinner.getSelectedItem());
						gameRules.addPlayer(playerToBeAdded, true);

						//add other players to the playing players if they are not "none"
						if(aiSpinner1.getSelectedItem().equals("none"))
							Log.d(TAG,"2nd player is none");
						else {
							playerToBeAdded = getPlayerProfile((String) aiSpinner1.getSelectedItem());
							gameRules.addTurn(gameRules.getNumberOfPlayingPlayers());
							gameRules.addPlayer(playerToBeAdded, false);
						}
						if(aiSpinner2.getSelectedItem().equals("none"))
							Log.d(TAG,"3rd player is none");
						else {
							playerToBeAdded = getPlayerProfile((String) aiSpinner2.getSelectedItem());
							gameRules.addTurn(gameRules.getNumberOfPlayingPlayers());
							gameRules.addPlayer(playerToBeAdded, false);
						}
						if(aiSpinner3.getSelectedItem().equals("none"))
							Log.d(TAG,"4th player is none");
						else {
							playerToBeAdded = getPlayerProfile((String) aiSpinner3.getSelectedItem());
							gameRules.addTurn(gameRules.getNumberOfPlayingPlayers());
							gameRules.addPlayer(playerToBeAdded, false);

						}
						Log.d(TAG,"gameRules.getNumberOfPlayingPlayers() "+gameRules.getNumberOfPlayingPlayers());
						Toast.makeText(getApplicationContext(), "Play Now !!!", Toast.LENGTH_SHORT).show();

						//initialise intial turn order
						initInitialTurnOrder();
						//					gameRules.updateCurrentPlayerIndex(); 

						//set the dimensions to be used in the formation of image views and hence the grid view
						StringConstants.LINEAR_LAYOUT_HEIGHT = viewFlipper.getWidth();
						StringConstants.LINEAR_LAYOUT_WIDTH= viewFlipper.getWidth();

						Log.d(TAG,"viewFlipper params "+StringConstants.LINEAR_LAYOUT_HEIGHT + " "+StringConstants.LINEAR_LAYOUT_WIDTH);
						TextView playernameTextView = (TextView)findViewById(R.id.playerName);
						playernameTextView.setText(gameRules.getCurrentPlayerBoard().getPlayerProfile().getName());

						initPatentTechGridView();
						initProductiseGridView(6);
						initProductiseGridView(4);
						initRecruitEnggPanel();
						//					advertiseScreenTransparency();
						initFactoryPanel();
						currentEngineeringBVFactory(gameRules.getCurrentPlayerBoard());
						initPlayerInfo();
						initPlayerTechCards(gameRules.getCurrentPlayerBoard());
						initPlayerDirectors(gameRules.getCurrentPlayerBoard());
						GlobalPreferences.stopSound();
						Log.d(TAG,"  gameRules.getNumberOfPlayingPlayers()" +gameRules.getNumberOfPlayingPlayers() );
						dialog.dismiss();
					}
				}
			}
		});
		GlobalPreferences.playSound(getApplicationContext(), R.raw.technodo_start);
		dialog.show();

	}

	public void advertiseScreenTransparency(){
		for (int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++){
				if(gameRules.getSubsidy() == j && gameRules.getAdvertisedRows() == i)   
				{

					View currentImageView = getGridElementView(i, j,4);
					Log.d(TAG,"row and column "+ i + j +" alpha "+currentImageView.getId());
					Log.d(TAG,"row and column "+ gameRules.getSubsidy() + gameRules.getAdvertisedRows());

					baseImage = (ImageView) currentImageView.findViewById(R.id.base);
					//				baseImage.setAlpha(90);
					baseImage.setVisibility(View.INVISIBLE);
				}
			}
		}
	}
	/**
	 * 
	 * @param position : index for which the player profile is required
	 * @return : required player profile 
	 */
	public PlayerProfile getPlayerProfile(int position) {
		return playerProfiles[position];
	}

	/**
	 * Find a player profile matching the name
	 * @param name to be matched against name of the player
	 * @return PlayerProfile if found
	 */
	public PlayerProfile getPlayerProfile(String name) {
		for(PlayerProfile p : playerProfiles){
			if(p.getTitle().equalsIgnoreCase(name) == true)
			{
				return p;
			}
		}
		return null;
	}


	private void setHumanImage(int position) {
		ArrayList<String> path =new ArrayList<String>();
		for(PlayerProfile p : playerProfiles){
			if(p.getPlayerType() == PlayerType.HUMAN)
				path.add(p.getIconPath());
		}
		person_1_ImageView = (ImageView) dialog.findViewById(R.id.person1);
		//		Log.d(TAG, "person_1_ImageView  :"+GlobalPreferences.getGlobalPreferences().getDataDir()+GET_PATH+path.get(position));
		placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+GET_PATH+path.get(position), person_1_ImageView);
		//Adding image of human player on right side pane
	}

	private void setAIImage(int SpinnerNumber, int position) {
		//		Log.d(TAG,"method called for spinner number : "+SpinnerNumber);
		ArrayList<String> path =new ArrayList<String>();
		for(PlayerProfile p : playerProfiles){
			path.add(p.getIconPath());
		}
		person_2_ImageView = (ImageView) dialog.findViewById(R.id.person2);
		person_3_ImageView = (ImageView) dialog.findViewById(R.id.person3);
		person_4_ImageView = (ImageView) dialog.findViewById(R.id.person4);
		player2ImageView = (ImageView)findViewById(R.id.button2);
		player3ImageView = (ImageView)findViewById(R.id.button3);
		player4ImageView = (ImageView)findViewById(R.id.button4);
		Bitmap bitmap=null;
		//check is required so that if a none is chosen the code do not crash
		if(position < path.size())
			bitmap = BitmapFactory.decodeFile(GlobalPreferences.getGlobalPreferences().getDataDir()+GET_PATH+path.get(position));

		switch (SpinnerNumber){
		case 1:
			if(null!=bitmap)
				placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+GET_PATH+path.get(position), person_2_ImageView);
			else{
				person_2_ImageView.setImageResource(R.drawable.none);
				player2ImageView.setImageResource(R.drawable.none);
			}
			break; 
		case 2:
			if(null!=bitmap)
				placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+GET_PATH+path.get(position), person_3_ImageView);
			else{
				person_3_ImageView.setImageResource(R.drawable.none);
				player3ImageView.setImageResource(R.drawable.none);
			}
			break;
		case 3:
			if(null!=bitmap)
				placeImages(GlobalPreferences.getGlobalPreferences().getDataDir()+GET_PATH+path.get(position), person_4_ImageView);
			else{ 
				person_4_ImageView.setImageResource(R.drawable.none);
				player4ImageView.setImageResource(R.drawable.none);
			}
			break;
		}

	}

	/**
	 * 
	 * @param row : each player will occupy a row in the layout)
	 * @param COLUMNS : 1st column : player image and the next levels will show the level of factory 
	 * @return : view of each item in factory_level.xml
	 */
	public ImageView getFactoryView( int row,int column){
		ImageView temp = null ;
		switch (row){
		case 0: 
			switch (column){
			case 0 : temp = (ImageView) findViewById(R.id.factoryIcon00);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.factoryIcon01);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.factoryIcon02);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.factoryIcon03);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;

		case 1:
			switch (column){
			case 0 : temp = (ImageView)findViewById(R.id.factoryIcon10);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.factoryIcon11);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.factoryIcon12);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.factoryIcon13);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;
		case 2:
			switch (column){
			case 0 : temp = (ImageView)findViewById(R.id.factoryIcon20);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.factoryIcon21);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.factoryIcon22);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.factoryIcon23);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;
		case 3:
			switch (column){
			case 0 : temp = (ImageView)findViewById(R.id.factoryIcon30);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.factoryIcon31);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.factoryIcon32);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.factoryIcon33);
			break;
			default : Log.d(TAG,"unknown column value found");
			}
			break;
		default : Log.d(TAG,"unknown row value found");
		}
		//		Log.d(TAG,"inside returning getImageView");
		return temp;
	}
	/**
	 * 
	 * @param row : each player will occupy a row in the layout)
	 * @param COLUMNS : 1st column : player image and the next levels will show the level of engg 
	 * @return : view of each item in recruit_engg.xml
	 */
	public ImageView getRecruitEnggView( int row,int column){
		ImageView temp = null ;
		switch (row){
		case 0: 
			switch (column){
			case 0 : temp = (ImageView) findViewById(R.id.enggIcon00);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.enggIcon01);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.enggIcon02);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.enggIcon03);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;

		case 1:
			switch (column){
			case 0 : temp = (ImageView)findViewById(R.id.enggIcon10);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.enggIcon11);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.enggIcon12);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.enggIcon13);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;
		case 2:
			switch (column){
			case 0 : temp = (ImageView)findViewById(R.id.enggIcon20);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.enggIcon21);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.enggIcon22);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.enggIcon23);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;
		case 3:
			switch (column){
			case 0 : temp = (ImageView)findViewById(R.id.enggIcon30);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.enggIcon31);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.enggIcon32);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.enggIcon33);
			break;
			default : Log.d(TAG,"unknown column value found");
			}
			break;
		default : Log.d(TAG,"unknown row value found");
		}
		//		Log.d(TAG,"inside returning getImageView");
		return temp;
	}

	/**
	 * 
	 * @param row : row of selected item in  the player panel (ROWS : player credentials, techs, management, etc )
	 * @param column : column of selected item in  the player panel 
	 * @return : view of the item selected from the player panel
	 */
	public ImageView getPlayerTechCardView( int row,int column){
		ImageView temp = null ;
		switch (row){
		case 0: 
			switch (column){
			case 0 : temp = (ImageView) findViewById(R.id.button1);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.button2);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.button3);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.button4);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;

		case 1:
			switch (column){
			case 0 : temp = (ImageView)findViewById(R.id.button5);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.button6);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.button7);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.button8);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;
		case 2:
			switch (column){
			case 0 : temp = (ImageView)findViewById(R.id.button9);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.button10);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.button11);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.button12);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;
		case 3:
			switch (column){
			case 0 : temp = (ImageView)findViewById(R.id.button13);
			break;
			case 1 : temp = (ImageView)findViewById(R.id.button14);
			break;
			case 2 : temp = (ImageView)findViewById(R.id.button15);
			break;
			case 3 : temp = (ImageView)findViewById(R.id.button16);
			break;
			default : Log.d(TAG,"unknown column value found");
			}
			break;
		default : Log.d(TAG,"unknown row value found");
		}
		//		Log.d(TAG,"inside returning getImageView");
		return temp;
	}

	/**
	 * 
	 * @param row : row of selected item in  the player panel (ROWS : player credentials, techs, management, etc )
	 * @param column : column of selected item in  the player panel 
	 * @param viewNumber : depending on the lower panel icon which is chosen it will change the respective view
	 * @return : view of the tech or product selected from the grid
	 */
	public View getGridElementView( int row,int column, int viewNumber){
		//		Log.d(TAG,"inside getImageView");
		View tempView = null ;
		View v = null;

		//set the view for which the item has been selected
		switch(viewNumber){
		case 0:
			v = findViewById(R.id.techCardPanel);
			break;
		case 6:
			v = findViewById(R.id.productisePanel);
			break;
		case 4:
			v = findViewById(R.id.adveritisePanel);
			break;
		default : Log.d(TAG,"wrong view selected");
		}

		//points to the exact element
		switch (row){
		case 0: 
			switch (column){
			case 0 : tempView = (View) v.findViewById(R.id.imageView00);
			break;
			case 1 : tempView = (View) v.findViewById(R.id.imageView01);
			break;
			case 2 : tempView = (View)v.findViewById(R.id.imageView02);
			break;
			case 3 : tempView = (View)v.findViewById(R.id.imageView03);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;

		case 1:
			switch (column){
			case 0 : tempView = (View) v.findViewById(R.id.imageView10);
			break;
			case 1 : tempView = (View) v.findViewById(R.id.imageView11);
			break;
			case 2 : tempView = (View) v.findViewById(R.id.imageView12);
			break;
			case 3 : tempView = (View) v.findViewById(R.id.imageView13);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;
		case 2:
			switch (column){
			case 0 : tempView = (View) v.findViewById(R.id.imageView20);
			break;
			case 1 : tempView =(View) v.findViewById(R.id.imageView21);
			break;
			case 2 : tempView = (View) v.findViewById(R.id.imageView22);
			break;
			case 3 : tempView = (View) v.findViewById(R.id.imageView23);
			break;
			default : Log.d(TAG,"unknown row value found");
			}
			break;
		case 3:
			switch (column){
			case 0 : tempView = (View) v.findViewById(R.id.imageView30);
			break;
			case 1 : tempView = (View) v.findViewById(R.id.imageView31);
			break;
			case 2 : tempView = (View) v.findViewById(R.id.imageView32);
			break;
			case 3 : tempView = (View) v.findViewById(R.id.imageView33);
			break;
			default : Log.d(TAG,"unknown column value found");
			}
			break;
		default : Log.d(TAG,"unknown row value found");
		}
		return tempView;
	}
	/**
	 * initialise the cost cut panel
	 */
	private void initCostCutPanel(){
		final TextView costCutProfit;
		final TextView costCutBV;
		final Button buttonCostCut;

		buttonCostCut = (Button) findViewById(R.id.buttonCostCut);
		costCutProfit = (TextView)findViewById(R.id.costCutProfit);
		costCutBV = (TextView)findViewById(R.id.costCutBV);
		costSeekBar =(SeekBar) findViewById(R.id.costCut);
		costSeekBar.setMax(gameRules.getCurrentPlayerBoard().getBrandValue());

		costSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub

			}

			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub

			}

			public void onProgressChanged(SeekBar arg0,int progress, boolean arg2) {
				// TODO Auto-generated method stub
				costCutProfit.setText(progress*4 + "Million");
				costCutBV.setText("-"+progress+" BV");
				cost = progress;
			}
		});
		buttonCostCut.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Log.d(TAG,"cost cut button clicked" + cost);
				if(cost>0)
					gameRules.ActionCostCut(gameRules.getCurrentPlayerBoard(), cost);

			}
		});


	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_technodo_main, menu);
		return true;
	}
	/**
	 * Loads player profiles from the jsons for selection
	 * @throws JSONException 
	 * @throws IllegalArgumentException 
	 */
	private void loadPlayerProfiles() throws IllegalArgumentException, JSONException {
		String []profiles = GlobalPreferences.getGlobalPreferences().getProfiles();
		int i = 0;
		playerProfiles = new PlayerProfile[profiles.length];
		for(String pFiles : profiles) {
			playerProfiles[i] = new PlayerProfile(pFiles);
			i = i + 1;
		}

	}
}
